####################################
# RDD plots gender
####################################

rm(list=ls())

library(Hmisc)
library(ggplot2)
library(stargazer)
library(foreign)
library(rdrobust) #version 0.99.4
library(rdd)
library(readstata13)
library(gridExtra)

################
# Prepare data 
################

# read data
load("~/Dropbox/Gender Chile/08_replication/gender_chile_2020aug28.RData")  
names(d)

# Bandwidth
h_bw = rdbwselect(d$permanent_temporary,d$margin,cluster=d$cluster)$bws[1]

#########################
# Main RDD plots 
#########################

# Permanent and temporary
summary(rdrobust(d$permanent_temporary,d$margin,cluster = d$cluster,all=TRUE))

(rdplot(y = d$permanent_temporary, x = d$margin,  h= 0.091, nbins = 1000, subset = -0.091 <= d$margin & d$margin <= 0.091, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0,300), x.lim = c(-0.10,0.10), title = "Robust CI: [-257.880,-75.321]",
        y.label = "Municipal employees", x.label = "Women candidates' margin of victory"))


# Female permanent + temporary
summary(rdrobust(d$female_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE))

(rdplot(y = d$female_permanent_temporary, x = d$margin,  h= 0.086, nbins = 1000, subset = -0.086 <= d$margin & d$margin <= 0.086, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0,300), title = "Robust CI: [-101.964,-21.141]",
        y.label = "Women municipal employees", x.label = "Women candidates' margin of victory"))


# Male permanent + temporary
summary(rdrobust(d$male_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE))

(rdplot(y = d$male_permanent_temporary, x = d$margin,  h= 0.096, nbins = 1000, subset = -0.096 <= d$margin & d$margin <= 0.096, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0,300), title = "Robust CI: [-152.610,-49.276]",
        y.label = "Men municipal employees", x.label = "Women candidates' margin of victory"))

# Share permanent + temporary
summary(rdrobust(d$sharefemale_permanent_temporary,d$margin,cluster = d$cluster,all=TRUE))

(rdplot(y = d$sharefemale_permanent_temporary, x = d$margin,  h= 0.166, nbins = 1000, subset = -0.166 <= d$margin & d$margin <= 0.166, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0, 1), title = "Robust CI: [0.042,0.121]",
        y.label = "Share of municipal employees", x.label = "Women candidates' margin of victory"))

